package example.Fadenkreuz;
import java.awt.*;
import java.awt.event.*;

import javax.media.opengl.*;



public class MyFirst  implements WindowListener, GLEventListener {
	
	private GLCanvas canvas;		//OpenGL-Canvas
	double left = -10, right = 10;	//Koordinatenbereich
	double bottom, top;
	double near = -10;				//Clipping Bereich
	double far = 10;
	int mx = 0;
	int my = 0;
	
	private void zeichneAchsen(GL gl){	//Koordinatenachsen zeichnen
		gl.glBegin(GL.GL_LINES);
		gl.glVertex2d(left, 0);
		gl.glVertex2d(right, 0);
		gl.glVertex2d(0, bottom);
		gl.glVertex2d(0, top);
		gl.glEnd();
	}
	
	
	private void zeichneKreis(GL gl, double xm, double ym, double r, int nPkte){
		double phi = 2*Math.PI/nPkte;
		gl.glBegin(GL.GL_POLYGON);
		for(int i=0; i < nPkte; i++){
			gl.glVertex2d(xm + r * Math.cos(i * phi), ym + r*Math.sin(i * phi));
		}
		gl.glEnd();
	}
	
	public MyFirst(){
		Frame f = new Frame("MyFirst");
		canvas = new GLCanvas();
		f.setSize(480, 320);
		f.setBackground(Color.gray);
		f.addWindowListener(this);
		canvas.addGLEventListener(this);
		f.add(canvas);
		f.setVisible(true);
	}
	
	public static void main(String[] args){
		new MyFirst();
	}

	@Override
	public void display(GLAutoDrawable drawable) {
		final GL gl = drawable.getGL();
		gl.glClear(GL.GL_COLOR_BUFFER_BIT);
		gl.glColor3d(0, 1, 1);
		gl.glPolygonMode(GL.GL_FRONT_AND_BACK, GL.GL_LINE);
		zeichneAchsen(gl);
		zeichneKreis(gl, 0, 0, 3, 40);
		zeichneKreis(gl, 0, 0, 1.5, 40);
	}

	@Override
	public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}

	@Override
	public void init(GLAutoDrawable drawable) {
		GL gl = drawable.getGL();
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	}

	@Override
	public void reshape(GLAutoDrawable drawable, int x, int y, int width,
			int height) {
		GL gl = drawable.getGL();
		double yxRatio = (float)height/width;
		bottom = yxRatio * left;
		top = yxRatio * right;
		gl.glMatrixMode(GL.GL_PROJECTION);
		gl.glLoadIdentity();
		gl.glOrtho(left, right, bottom, top, near, far);
		gl.glViewport(0, 0, width, height);
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
